package com.example.wzh.rxdemo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;

import com.example.wzh.rxdemo.databinding.ItemOperatorBinding;

import java.util.List;

/**
 * @author: zhwang37
 * @date: 2021/8/25 14:48
 * @email:zhwang37@iflytek.com
 * @describe:
 */
public class OperatorAdapter extends Adapter<OperatorAdapter.ViewHolder> {
    List<String> operatorList;
    private OnItemClickListener onItemClickListener;

    public OperatorAdapter(List<String> operatorList) {
        this.operatorList = operatorList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemOperatorBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                , R.layout.item_operator, parent, false);
        return new ViewHolder(binding.getRoot());
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.binding.txt.setText(operatorList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return operatorList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ItemOperatorBinding binding;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.getBinding(itemView);
        }
    }
}
